home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Libraries / JPEG Library / JPEGTestApp.c next >
Encoding:
C/C++ Source or Header  |  1994-07-12  |  4.1 KB  |  123 lines  |  [TEXT/MPCC]

  1. //=====================================================================================
  2. // JPEGUtilities.c -- written by Aaron Giles
  3. // Last update: 7/7/94
  4. //=====================================================================================
  5. // A very simple, poorly-coded demo application which opens JPEG and PICT images in a
  6. // window, then converts from one format to the other, displaying the result in a 
  7. // second window.  No real user interface is presented.  After displaying the first
  8. // image, click the mouse button to perform the JPEG<->PICT conversion and view the
  9. // resulting image.  Click the mouse button again to quit.
  10. //=====================================================================================
  11. // This code has been successfully compiled under Metrowerks C/C++ 1.0a4, under
  12. // THINK C 7.0, and under MPW C 3.3.
  13. //=====================================================================================
  14. // If you find any bugs in this source code, please email me and I will attempt to fix
  15. // them.  If you have any additions/modifications that you think would be generally
  16. // useful, email those to me as well, and I will consider incorporating them into the
  17. // next release.  My email address is giles@med.cornell.edu.
  18. //=====================================================================================
  19. // This source code is copyright © 1994, Aaron Giles.  Permission to use this code in
  20. // your product is freely granted, provided that you credit me appropriately in your
  21. // application's About box/credits *and* documentation.  If you ship an application
  22. // which uses this code, I would also like to request that you provide me with one
  23. // complimentary copy of the application.
  24. //=====================================================================================
  25.  
  26. #include <Errors.h>
  27. #include <Fonts.h>
  28. #include <Memory.h>
  29. #include <QuickDraw.h>
  30. #include <StandardFile.h>
  31. #include <Types.h>
  32.  
  33. #include "JPEGUtilities.h"
  34.  
  35. void main(void);
  36. OSErr ReadData(StandardFileReply *reply, Handle *theData);
  37. OSErr DrawImageInWindow(Handle theData, OSType theType);
  38.  
  39. void main(void)
  40. {
  41.     OSType theTypes[] = { 'PICT', 'JPEG' };
  42.     StandardFileReply reply;
  43.     Handle theData, newData;
  44.     OSErr theErr;
  45.  
  46.     InitGraf(&qd.thePort);
  47.     InitFonts();
  48.     InitWindows();
  49.     InitMenus();
  50.     TEInit();
  51.     InitDialogs(nil);
  52.     InitCursor();
  53.  
  54.     StandardGetFile(nil, 2, theTypes, &reply);
  55.     if (reply.sfGood) {
  56.         theErr = ReadData(&reply, &theData);
  57.         if (theErr == noErr) theErr = DrawImageInWindow(theData, reply.sfType);
  58.         if (theErr == noErr) {
  59.             while (!Button());
  60.             if (reply.sfType == 'PICT') {
  61.                 newData = UnwrapJPEG((PicHandle)theData);
  62.                 reply.sfType = 'JPEG';
  63.             } else {
  64.                 newData = (Handle)WrapJPEG(theData);
  65.                 reply.sfType = 'PICT';
  66.             }
  67.             if (newData) {
  68.                 theErr = DrawImageInWindow(newData, reply.sfType);
  69.                 while (!Button());
  70.             } else SysBeep(1);
  71.         }
  72.     }
  73. }
  74.         
  75. OSErr ReadData(StandardFileReply *reply, Handle *theData)
  76. {
  77.     short refNum;
  78.     OSErr theErr;
  79.     long size;
  80.     
  81.     theErr = FSpOpenDF(&reply->sfFile, fsRdPerm, &refNum);
  82.     if (theErr == noErr) {
  83.         theErr = GetEOF(refNum, &size);
  84.         if (theErr == noErr && reply->sfType == 'PICT') {
  85.             size -= 512;
  86.             theErr = SetFPos(refNum, fsFromStart, 512);
  87.         }
  88.         if (theErr == noErr) {
  89.             if (*theData = NewHandle(size)) {
  90.                 HLock(*theData);
  91.                 theErr = FSRead(refNum, &size, **theData);
  92.                 HUnlock(*theData);
  93.             } else theErr = memFullErr;
  94.         }
  95.         FSClose(refNum);
  96.     }
  97.     return theErr;
  98. }
  99.  
  100. OSErr DrawImageInWindow(Handle theData, OSType theType)
  101. {
  102.     OSErr theErr = noErr;
  103.     WindowPtr theWindow;
  104.     Rect bounds;
  105.     
  106.     if (theType == 'PICT') bounds = (*(PicHandle)theData)->picFrame;
  107.     else theErr = GetJPEGBounds(theData, &bounds);
  108.     if (theErr == noErr) {
  109.         OffsetRect(&bounds, 0, 39);
  110.         theWindow = NewCWindow(nil, &bounds,
  111.                     (theType == 'PICT') ? (StringPtr)"\pPICT Image" : (StringPtr)"\pJPEG Image",
  112.                     true, noGrowDocProc, (WindowPtr)-1, false, 0);
  113.         if (theWindow) {
  114.             SetPort(theWindow);
  115.             OffsetRect(&bounds, 0, -39);
  116.             ClipRect(&bounds);
  117.             if (theType == 'PICT') DrawPicture((PicHandle)theData, &bounds);
  118.             else theErr = DrawJPEG(theData, &bounds, &bounds, ditherCopy);
  119.         }
  120.     }
  121.     return theErr;
  122. }
  123.